from bs4 import BeautifulSoup
from selenium import webdriver
from collections import OrderedDict
import json
import time
import os

# # xls文件下载路径
# path = "F:\data"
# if not os.path.exists(path):
#     os.makedirs(path)

def read_json(f_json):
     # 读取存储于json文件中的列表
        fp = open(f_json, 'r',encoding="utf8")
        jlist = json.load(fp)
        return jlist

options = webdriver.ChromeOptions()
driver = webdriver.Chrome(chrome_options=options)
driver.get('https://uac.10010.com/portal/homeLoginNew')

# 输入手机号码
while True:
    try:
        phoneInput = driver.find_element_by_xpath("//input[@class = 'iptStyle width250 username']")
        phoneInput.clear()
        break
    except:
        continue
phoneNum = input("手机号码：")
# phoneNum = "13173625819"
for num in phoneNum:
    phoneInput.send_keys(num)

# 获取短信验证码按钮
while True:
    try:
        driver.find_element_by_xpath("//span[@class = 'randomSpan ie6Png countdown']").click()
        break
    except:
        continue

serverCode = input("服务密码：")
# serverCode = '123589'
# 159753
severCodeInput = driver.find_elements_by_xpath("//input[@class = 'iptStyle width145 password']")[0]
severCodeInput.clear()
for num in serverCode:
    severCodeInput.send_keys(num)

messageCode1 = input("短信验证码：")
messageCodeInput1 = driver.find_elements_by_xpath("//input[@class = 'iptStyle width145 password']")[1]
messageCodeInput1.clear()
for num in messageCode1:
    messageCodeInput1.send_keys(num)
# 登录按钮
driver.find_element_by_xpath("//input[@class = 'loginBtn ie6Png mtr40 width302']").click()

# 查询按钮，跳转至 https://iservice.10010.com/e5/query.html
while True:
    try:
        driver.find_element_by_xpath("//a[@id = 'query']").click()
        break
    except:
        continue
# 始终获得当前最后的窗口
for handle in driver.window_handles:
    driver.switch_to.window(handle)
# print(driver.current_url)

# 通话详单按钮，跳转至 https://iservice.10010.com/e4/query/bill/call_dan-iframe.html
while True:
    try:
        driver.find_element_by_xpath("//div[@id = 'NavId']/div[@class = 'NavDivs width1200 bgFFF']/ul[@class = 'fl threeNav']/li/a[@title = '通话详单']").click()
        break
    except:
        continue

# 始终获得当前最后的窗口
for handle in driver.window_handles:
    driver.switch_to.window(handle)
# print(driver.current_url)

# 获取验证码按钮
while True:
    try:
        driver.find_element_by_xpath("//div[@id = 'huoqu_buttons']").click()
        break
    except:
        continue

messageCode2 = input("验证码：")
messageCode2Input = driver.find_element_by_xpath("//input[@id = 'input']")
for num in messageCode2:
    messageCode2Input.send_keys(num)
# 登录按钮
driver.find_element_by_xpath("//input[@id = 'sign_in']").click()

while True:
    try:
        monthsBtn = driver.find_elements_by_xpath("//ul[@class = 'score_list_ul score_list_min ']/li")
        break
    except:
        continue

for i in range(0, 6):
    # 月份选择按钮
    try:
        monthsBtn[i].click()
    except:
        pass
    # 滑动到页面底部
    js = "var q=document.documentElement.scrollTo(0,document.body.scrollHeight)"
    driver.execute_script(js)
    # 导出按钮
    while True:
        try:
            if driver.find_element_by_xpath("/html/body/div[1]/div[4]/div[4]/div/div[5]/div[2]/div/div[1]/div[3]/div[2]/div[4]/div[2]/div[3]/div[1]/div[5]/input[2]"):
                break
        except:
            # 无详单记录【2114030170】
            try:
                if driver.find_element_by_xpath("/html/body/div[1]/div[4]/div[4]/div/div[5]/div[2]/div/div[1]/div[3]/div[2]/div[4]/div[2]/div[3]/div[2]/div/p[1]"):
                    break
            except:
                pass
    # 滑动到页面顶部
    js = "var q=document.documentElement.scrollTop=0"
    driver.execute_script(js)
    #写入网页
    # 函数化
    time.sleep(5)
    get_html = "tset"+str(i)+".html"
    f = open(get_html,'wb')
    f.write(driver.page_source.encode("utf-8"))
    # print("fine")
    f.close()
    #打开网页，使用Beautiful soup 分析
    html = open(get_html,'r', encoding='UTF-8')
    soup = BeautifulSoup(html, 'lxml')
    list_1 = []
    for th in soup.find_all('th'):
        # print(th.text)
        list_1.append(th.text)

    d = OrderedDict()
    #有序字典
    dictList = []
    # list_0 = ["通话起始时间" ,"通话时长" ,"呼叫类型" ,"对方号码" ,"本机通话地" ,"对方归属地" ,"通话类型" ,"业务类型" ,"通话费"]
    m = int(len(list_1 ) /9)
    for j in range(0 ,m):
        print(j)
        if j <= (m-2):
            for k in range(0,9):
                key = list_1[k]
                key = key.replace("\t\t                  ",'')
                d[key] = list_1[( k +( 9 *(j+1)))]
            print(d)
            dictList.append(d)
            d = OrderedDict()
        else:
            pass

    # 生成json文件
    get_json = "json"+str(i)+".json"
    fj = open(get_json,"w", encoding="utf8")
    json.dump(dictList, fj, ensure_ascii=False, indent=4)
    # print("fine_1")
    fj.close()
    html = ''
    soup = ''
    list_1 = ''

#driver.close()


